<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Jump</title>
    <style>
        .back{
            width: 60px;
            height: 100px;
            text-align: center;
            line-height: 100px;
            background-color: #cccccc;
            position: fixed;
            bottom: 30px;
            right: 20px;
            display: none;
        }

        .content{
            height: 2000px;
        }
    </style>
</head>
<body>
<div class="content">
    <h1>浪子回头金不换，该回头时就回头！</h1>
    <h3>有本事你往下走呀</h3>
</div>

<div class="back">
    返回
</div>
<script>
    let back = document.getElementsByClassName('back')[0];
    window.onscroll=function () {
        //获取页面滚动条的纵坐标 https://segmentfault.com/a/1190000008065472
         // let res=document.body.scrollTop||document.documentElement.scrollTop;
         // let res=document.documentElement.scrollTop;
        let res=window.pageYOffset
         console.log(res)

        if (res > 500) {
            back.style.display = 'block';
            back.onclick=function () {
                document.documentElement.scrollTop=0;
                // window.pageYOffset=0;
            }
        }else {
            back.style.display='none'
        }
    }
</script>
</body>
</html>